<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Users;
use App\Models\Linkslists;
use App\Models\Memberorders;
use App\Models\Linkslistssetting;
use App\Models\Linkslistskefu;
use App\Models\Linksrecord;
use App\Models\Linksliststotal;
use App\Services\DeviceService;
use App\Services\EmailService;
use DB,Log,Exception;

class Linksupdate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'links:update';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '版本功能升级更新数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->row = 1;
        $this->textFile = "";
        $this->start_memory = 1;
        $this->links_id = "CmfHaNbSNS";
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $linkRecordDb = new Linksrecord(["tbnum"=>3]);
        $a = $linkRecordDb->getLinkRecordDetial("a94652f6baf8f1ce4b6d4adf38c1aa40");
        dd($a);
        // dump("------------------\n创建累计数据统计Start\n------------------\n");
        // $id = 1;
        // // 更新所有会员的到期时间到users
        // DB::table("links_lists")->where("id",">=",$id)->orderBy("id","ASC")->chunk(100, function ($links_lists) {
        //     $linkListTotal = [];
        //     foreach ($links_lists as $links_list) {
        //         dump($links_list->id);
        //         $static = DB::select('select SUM(page_view) as page_view,SUM(mini_view) as mini_view,SUM(mini_direct) as mini_direct,SUM(mini_activate) as mini_press,SUM(mini_card) as mini_card,links_id from links_lists_statistic where `links_id` = "'.$links_list->links_id.'"');
        //         if (!empty($static[0]->page_view) || !empty($static[0]->mini_view)) {
        //             $linkListTotal[] = (array)$static[0];
        //         }
        //     }
        //     Linksliststotal::createLinkListTotalBatch($linkListTotal);
        // });
        // dd("------------------\n创建累计数据统计End\n------------------\n");

        // 6月份的数据ID
        // $six_id = 56302508;
        $six_id = 58730787;
        // 7月份的数据ID
        // $seven_id = 58311130;
        $seven_id = 58731572;
        DB::table("links_lists_ips")->where("id",">=",$six_id)->where("id","<",$seven_id)->orderBy("id","ASC")->chunk(100, function ($links_lists_ips) {
            $linksRecords = [];
            dump("处理ID:".$links_lists_ips[0]->id."\n");
            foreach ($links_lists_ips as $linksIp) {
                if ($linksIp->status == 1) {
                    $web_time = $linksIp->created_at;
                    $mini_open_time = 1;
                    $mini_show_time = 1;
                } elseif ($linksIp->status == 2) {
                    $web_time = $linksIp->created_at;
                    $mini_open_time = $linksIp->created_at+2;
                    $mini_show_time = $linksIp->created_at+3;
                } else {
                    $web_time = 1;
                    $mini_open_time = $linksIp->created_at-1;
                    $mini_show_time = $linksIp->created_at;
                }

                if ($linksIp->active == 1) {
                    $mini_press_time = 1;
                } else {
                    $mini_press_time = strtotime($linksIp->updated_at);
                }
                $record_id = md5($linksIp->id);
                $linksRecords[] = ["id"=>$linksIp->id,"record_id"=>$record_id,"links_id"=>$linksIp->links_id,"province"=>$linksIp->province,"city"=>$linksIp->city,"system"=>$linksIp->system,"device"=>$linksIp->device,"model"=>$linksIp->model,"browser"=>$linksIp->browser,"ip"=>$linksIp->ip,"code"=>$linksIp->code,"qrcodes_id"=>"","web_time"=>$web_time,"mini_open_time"=>$mini_open_time,"mini_show_time"=>$mini_show_time,"mini_press_time"=>$mini_press_time,"created_at"=>$linksIp->created_at,"updated_at"=>$linksIp->updated_at];
            }
            try {
                $linkRecordDb = new Linksrecord(["tbnum"=>3]);
                $linkRecordDb->creatLinkRecordBatch($linksRecords);
            } catch (Exception $e) {
                echo "报错---\n";
            }
        });
    }
}
